package com.pan.studentmaster;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLayoutChangeListener;
import android.widget.GridLayout;
import android.widget.ImageButton;
import android.widget.GridLayout.Spec;
import android.widget.ImageView.ScaleType;

public abstract class GridActivity extends Activity implements OnLayoutChangeListener, OnClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_grid);
		_layout = (GridLayout)findViewById(R.id.container);
		_layout.addOnLayoutChangeListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public abstract Cursor getCursor();
	public abstract BitmapDrawable getDrawable(Cursor c);
	public abstract void onItemClick(View v);

	@Override
	public void onLayoutChange(View v, int left, int top, int right, int bottom,
			int oldLeft, int oldTop, int oldRight, int oldBottom) {
		if(_initialized)
			return;
		_initialized = true;
		int width = right - left;
		int height = bottom - top;
		int columns = width / EXPECT_WIDTH;
		int rows = height / EXPECT_HEIGHT;
		_layout.setRowCount(rows);
		_layout.setColumnCount(columns);
		int itemWidth = width / columns;
		int itemHeight = height / rows;
		Cursor cursor = getCursor();
		int row = 0;
		int column = 0;
		while(!cursor.isAfterLast()) {
			Spec rowSpec = GridLayout.spec(row, 1);
			Spec colSpec = GridLayout.spec(column, 1);
			if(++column >= columns) {
				++row;
				column = 0;
			}
			GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpec, colSpec);
			params.width = itemWidth;
			params.height = itemHeight;
			params.setGravity(Gravity.CENTER | Gravity.FILL);
			ImageButton btn = new ImageButton(this);
			btn.setId(cursor.getInt(cursor.getColumnIndex(DB.COURSE._ID)));
			btn.setBackgroundColor(0xffffff);
			btn.setScaleType(ScaleType.CENTER_INSIDE);
			BitmapDrawable drawable = getDrawable(cursor);
			if(drawable != null)
				btn.setImageDrawable(getDrawable(cursor));
			else
				btn.setImageResource(R.drawable.notfound);
			btn.setOnClickListener(this);
			_layout.addView(btn, params);
			cursor.moveToNext();
		}
		cursor.close();
	}

	@Override
	public void onClick(View v) { onItemClick(v); }
	
	// WIDTH & HEIGHT are expected
	// real item size are calculated evenly to match the screen size
	// else Android will put rest size to the last item
	static final private int		EXPECT_WIDTH = 150;
	static final private int		EXPECT_HEIGHT = 200;
	private GridLayout				_layout = null;
	private boolean					_initialized = false;
	
}
